package com.hualan.sfzyproject.interceptor;


import com.hualan.sfzyproject.util.R;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//登录拦截
public class Interceptor01 implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("Interceptor01执行了...");

        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            return true;
        }
        //登录认证
        //获取请求头中的token
        String token = request.getHeader("token");

        System.out.println("token为:" + token);

        System.out.println("请求路径为:"+request.getRequestURI());
        //判断是否有token
        if (token != null || request.getRequestURI().contains("/login") || request.getRequestURI().contains("/register")){
            return true;
        }else{
            R r = R.error2("请登录后再操作");

            return false;
        }


    }
}
